<?php
include '../common.php';

$db = new DB($config['db_host'], $config['db_name'], $config['db_user'], $config['db_pass']);
$req = file_get_contents('php://input');
$ret = json_decode($req);


switch ( $_SERVER['REQUEST_METHOD'] ) {

    // 增加
    case 'POST':
        $sql = 'insert into `tasks` (`title`) values ("'. $ret->title .'")';
        $r = $db->query($sql);
        echo json_encode($r);
        break;

    // 获取
    case 'GET':
        $sql = 'select * from `tasks`';
        $r = $db->get_results($sql);
        echo json_encode($r);
        break;

    // 更新
    case 'PUT':
        $sql = 'update `tasks` set `title` = "'. $ret->title .'" where `id` = ' . $ret->id;
        $r = $db->query($sql);
        echo json_encode($r);
        break;

    // 删除
    case 'DELETE':
        $sql = 'delete from `tasks` where `id` = ' . $_GET['id'];
        $r = $db->query($sql);
        echo json_encode($r);
        break;
}
